package com.etc.demo.service.kado02.impl;


import com.etc.common.EchartsModelVo;
import com.etc.common.ServiceException;
import com.etc.demo.dao.kado02.KADO02Dao;
import com.etc.demo.po.Project;
import com.etc.demo.service.kado02.IKADO02Service;
import com.etc.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class KADO02ServiceImpl implements IKADO02Service {
    private static final Logger logger = LoggerFactory.getLogger(KADO02ServiceImpl.class);
    @Autowired
    KADO02Dao mapper;

    @Override
    public EchartsModelVo findProject(HashMap params) throws ServiceException {

        if (params.get("from") != null && !"".equals(params.get("from"))) {
            String from = params.get("from").toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
            Date d = DateUtil.toDatefomartYM(from);
            params.put("from", d);

        } else {
            params.remove("from");
        }
        if (params.get("to") != null && !"".equals(params.get("to"))) {
            String to = params.get("to").toString();

            Date d = DateUtil.toDatefomartYM(to);
            params.put("to", d);


        } else {
            params.remove("to");
        }


        List<Project> plist =  mapper.selectByFormTo(params);


        if (plist.size() == 0) {
            throw new ServiceException("M0001");
        }
        EchartsModelVo vo = new EchartsModelVo();
        List<String> pslist = new ArrayList<>();
        List<Date> seria1 = new ArrayList<>();
        List<Date> seria2 = new ArrayList<>();
        plist.forEach(item -> {
            pslist.add(item.getProjectname());

            seria1.add(item.getActuallyenddate());
            //
            seria2.add(item.getPlanenddate());
        });


        vo.setyAxis(pslist);
        vo.setSeries1(seria1);
        vo.setSeries2(seria2);
        return vo;
    }
}
